package _2022.hot100._206_反转链表;

/**
 * @author： YHSimon
 * @date： 2022-05-11 13:38
 */
class ListNode{
    int val;
    ListNode next;
    ListNode(){}
    ListNode(int val){this.val=val;}
    ListNode(int val,ListNode next){this.val=val;this.next=next;}
}
public class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode preHead=new ListNode(-1);
        while(head!=null){
            ListNode tmp=preHead.next;
            ListNode nextNode=head.next;
            preHead.next=head;
            head.next=tmp;
            head=nextNode;
        }
        return preHead.next;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        ListNode node = s.reverseList(new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5))))));
        while (node!=null){
            System.out.print(node.val+" ");
            node=node.next;
        }
    }
}
